package com.zbkj.crmeb.merchant.api.controller;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zbkj.crmeb.merchant.admin.service.MerStoreProductService;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductMer;
import com.zbkj.crmeb.store.service.StoreProductMerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.store.dto.HomeActivityDTO;
import com.zbkj.crmeb.store.model.HomeActivity;
import com.zbkj.crmeb.store.service.HomeActivityService;
import com.zbkj.crmeb.store.service.HomeStyleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 个性化首页
 * @Description: 
 * @author daigb
 * @date 2021-09-25
 */
@RestController
@RequestMapping("api/front/mer/home/activity")
@Api(tags = "用户首页--活动")
public class FontHomeActivityController extends ApiBaseController {
    @Autowired
    HomeActivityService homeActivityService;
    @Autowired
    HomeStyleService homeStyleService;
    @Autowired
    private MerStoreProductService storeProductService;
    @Autowired
    private StoreProductMerService storeProductMerService;

    /**
     * 根据id查询
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public CommonResult<HomeActivityDTO> get(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, HttpServletRequest request) {
    	HomeActivity homeActivityDB = homeActivityService.getOne(Wrappers.<HomeActivity>lambdaQuery()
    			.eq(HomeActivity :: getMerId, getMerId(request))
    			.eq(HomeActivity :: getId, id)
    			.last("LIMIT 1")
    			);
    	if(null == homeActivityDB) {
    		return CommonResult.validateFailed("记录不存在");
    	}

        // 对象复制
        HomeActivityDTO dto = new HomeActivityDTO();
        BeanUtils.copyProperties(homeActivityDB, dto);
        dto.setHomeStryles(homeStyleService.listEntityByActivityId(homeActivityDB.getId()));
        if (CollectionUtils.isNotEmpty(dto.getHomeStryles())){
            dto.getHomeStryles().forEach(homeStyleDTO -> {
                if (Objects.equals(homeStyleDTO.getType(),5)){
                    List<Integer> ids = JSON.parseArray(homeStyleDTO.getStyle().get("ids").toString(),Integer.class);
                    if (CollectionUtils.isNotEmpty(ids)){
                        List<StoreProduct> storeProductList = storeProductService.list(new LambdaQueryWrapper<>(StoreProduct.class)
                                .in(StoreProduct::getId, ids).eq(StoreProduct::getIsShow, true).gt(StoreProduct::getStock, 0));
                        List<Integer> idsList = storeProductList.stream().map(StoreProduct::getId).collect(Collectors.toList());
                        //商户商品是否下架
                        List<Integer> productMerIds = new ArrayList<>();
                        if (CollectionUtils.isNotEmpty(idsList)){
                            List<StoreProductMer> productMerList = storeProductMerService.list(Wrappers.lambdaQuery(StoreProductMer.class)
                                    .in(StoreProductMer::getProductId,idsList)
                                    .eq(StoreProductMer::getIsNotShow,"1"));
                            productMerIds = productMerList.stream().map(StoreProductMer::getProductId).collect(Collectors.toList());
                        }
                        List<Integer> integerList = new ArrayList<>();
                        //商品展示需排序
                        List<Integer> finalProductMerIds = productMerIds;
                        ids.forEach(i -> {
                            if (idsList.contains(i) && !finalProductMerIds.contains(i)){
                                integerList.add(i);
                            }
                        });
                        homeStyleDTO.getStyle().put("ids",integerList);
                    }
                }
            });
        }
    	return CommonResult.success(dto);
    }
    
}
